<template>
  <div v-loading="loading" class="clearfix" style="height:700px;overflow:auto">
    <el-form ref="form" :model="form" label-width="100px" :rules="rules">
      <el-form-item label="标题" prop="tittle">
        <el-input v-model="form.tittle"></el-input>
      </el-form-item>
      <el-form-item label="封面" prop="cover">
        <uploadImg
          remark="头条封面"
          :list="photoList"
          typeCode="IMAGE_ADVERTISING_FILE"
          @changeadvert="changeadvert"
        />
        <!-- form.cover -->
      </el-form-item>
      <el-form-item label="发布人" prop="publisher">
        <el-input v-model="form.publisher" placeholder="发布人"></el-input>
      </el-form-item>
      <el-form-item label="是否推送" prop="push">
        <el-select v-model="form.push" placeholder="请选择">
          <el-option
            v-for="item in pushs"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否发布" prop="status">
        <el-select v-model="form.status" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="摘要" prop="summary">
        <el-input
          v-model="form.summary"
          type="textarea"
          placeholder="摘要"
          maxlength="30"
          show-word-limit
          resize="none"
        ></el-input>
      </el-form-item>
      <el-form-item label="头条内容" prop="dto">
        <Editor :content="form.dto" show-btn="true" @submitText="submitText" :photoSize="2048" />
      </el-form-item>
      <div style="display:flex;justify-content:center;">
        <el-button class="right" type="primary" @click="submitInfoMation">提 交</el-button>
      </div>
    </el-form>
  </div>
</template>
<script>
import uploadImg from "../commom/uploadImg";
import Editor from "~/components/commom/Editor";
import regular from "~/utils/regular";
export default {
  components: {
    uploadImg,
    Editor
  },
  data() {
    return {
      rules: {
        tittle: regular.require1,
        summary: regular.require1,
        dto: regular.require1,
        status: regular.require1,
        // cover: regular.require1,
        push: regular.require1,
        publisher: regular.require1
      },
      loading: false,
      photoList: [],
      pushs: [
        {
          label: "推送",
          value: true
        },
        {
          label: "暂存",
          value: false
        }
      ],
      options: [
        {
          label: "发布",
          value: true
        },
        {
          label: "暂存",
          value: false
        }
      ],
      form: {
        tittle: "",
        summary: "",
        status: "",
        releaseTime: "",
        dto: "",
        cover: "",
        push: "",
        publisher: "",
        dtoId: "",
        id:''
      },
      result: {}
    };
  },
  props: ["id"],
  mounted() {
    if (this.$props.id !== "create") {
      this.$post({
        url: "/infomanage/informations/getInformationDetails",
        data: {
          id: this.$props.id
        }
      }).then(res => {
        this.form.cover = res.item.cover;
        this.form.publisher = res.item.publisher;
        this.form.status = res.item.status;
        this.form.summary = res.item.summary;
        this.form.tittle = res.item.tittle;
        this.form.releaseTime = res.item.releaseTime;
        this.form.dto = res.item.dto.content;
        this.form.dtoId = res.item.dto.id;
        this.form.id = res.item.id;
        this.photoList = [{ fileUrl: res.item.cover }];
        this.result = JSON.parse(JSON.stringify(res));
      });
    }
  },
  methods: {
    changeadvert(res) {
      this.uploadImg = true;
      this.form.cover = res.fileUrl;
    },
    submitText(res) {
      this.form.dto = res.text;
    },
    getDate() {
      var date = new Date();
      var seperator1 = "-";
      var seperator2 = ":";
      var month = date.getMonth() + 1;
      var day = date.getDate();
      var hours = date.getHours();
      var minutes = date.getMinutes();
      var seconds = date.getSeconds();
      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if (day >= 0 && day <= 9) {
        day = "0" + day;
      }
      if (hours >= 0 && hours <= 9) {
        hours = "0" + hours;
      }
      if (minutes >= 0 && minutes <= 9) {
        minutes = "0" + minutes;
      }
      if (seconds >= 0 && seconds <= 9) {
        seconds = "0" + seconds;
      }
      var currentdate =
        date.getFullYear() +
        seperator1 +
        month +
        seperator1 +
        day +
        " " +
        hours +
        seperator2 +
        minutes +
        seperator2 +
        seconds;
      return currentdate;
    },
    getReleaseTime() {
      let time = "";
      if (this.form.status !== this.result.item.status) {
        if (this.form.status) {
          time = this.getDate();
        } else {
          time = "";
        }
      } else {
        time = "";
      }
      return time;
    },
    submitInfoMation() {
      let item = {};
      if (this.$props.id === "create") {
        item = {
          cover: this.form.cover,
          dto: {
            content: this.form.dto,
            typeCode: "INFORMATIONS"
          },
          publisher: this.form.publisher,
          status: this.form.status,
          summary: this.form.summary,
          tittle: this.form.tittle,
          releaseTime: this.form.status ? this.getDate() : "",
          push: this.form.status ? this.form.push : ""
        };
      } else {
        item = {
          id:this.form.id,
          cover: this.form.cover,
          publisher: this.form.publisher,
          releaseTime: this.getReleaseTime(),
          status: this.form.status,
          tittle: this.form.tittle,
          summary: this.form.summary
        };
      }
      this.$post({
        url:
          this.$props.id === "create"
            ? "/infomanage/informations/createInformations"
            : "/infomanage/informations/editInformation",
        data: {
          item: item,
          push: this.form.push
        }
      })
        .then(res => {
          this.$message({
            type: "success",
            message: "操作成功"
          });
          if (this.$props.id !== "create") {
            this.$post({
              url: "/infomanage/textInformations/editTextInformations",
              data: {
                item:{
                  id:this.form.dtoId,
                  content:this.form.dto
                }
              }
            })
              .then(res => {})
              .catch(err => {});
          }
          this.$emit("closeDialog");
        })
        .catch(err => {});
    }
  }
};
</script>